home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / eulisp / feel-075.lha / feel0.75 / AddOns / debug.c next >
C/C++ Source or Header  |  1992-06-08  |  6KB  |  230 lines

  1. /* */
  2. #ifndef lint
  3. static char *RcsId="$Id: debug.c,v 1.1 1992/06/08 15:23:56 pab Exp $";
  4. #endif
  5.  
  6. /* Functions for Debugging
  7.  * This file is part of YY-server of YYonX (1.2 Distribution)
  8.  * $Id: debug.c,v 1.1 1992/06/08 15:23:56 pab Exp $
  9.  */
  10.  
  11. /****************************************************************************
  12. ;;;
  13. ;;;  Copyright (C) 1989,1990,1991 Aoyama Gakuin University
  14. ;;;
  15. ;;;        All Rights Reserved
  16. ;;;
  17. ;;; This software is developed for the YY project of Aoyama Gakuin University.
  18. ;;; Permission to use, copy, modify, and distribute this software
  19. ;;; and its documentation for any purpose and without fee is hereby granted,
  20. ;;; provided that the above copyright notices appear in all copies and that
  21. ;;; both that copyright notice and this permission notice appear in 
  22. ;;; supporting documentation, and that the name of Aoyama Gakuin
  23. ;;; not be used in advertising or publicity pertaining to distribution of
  24. ;;; the software without specific, written prior permission.
  25. ;;;
  26. ;;; This software is made available AS IS, and Aoyama Gakuin makes no
  27. ;;; warranty about the software, its performance or its conformity to
  28. ;;; any specification. 
  29. ;;;
  30. ;;; To make a contact: Send E-mail to ida@csrl.aoyama.ac.jp for overall
  31. ;;; issues. To ask specific questions, send to the individual authors at
  32. ;;; csrl.aoyama.ac.jp. To request a mailing list, send E-mail to 
  33. ;;; yyonx-request@csrl.aoyama.ac.jp.
  34. ;;;
  35. ;;; Authors:
  36. ;;;   Version 1.0 90/09/26 by Keisuke 'Keiko' Tanaka
  37. ;;;                (keisuke@csrl.aoyama.ac.jp)
  38. ;;;   Version 2.0 90/08/27 by Keisuke 'Keiko' Tanaka
  39. ;;;            Page Mode Territory is supported
  40. ;;;   Version 2.3 90/11/05 by Keisuke 'Keiko' Tanaka
  41. ;;;            Copyright Notice is rewritten
  42. ;;;
  43. ****************************************************************************/
  44.  
  45. /****************************************************************************
  46.   $Revision: 1.1 $ Written by Keisuke 'Keiko' Tanaka
  47.   $Date: 1992/06/08 15:23:56 $
  48. ****************************************************************************/
  49.  
  50. #include <stdio.h>
  51. #include <strings.h>
  52. #include <sys/types.h>
  53. #include "yydefs.h"
  54.  
  55. #define MAXDEBUGSTACKLEVEL    30
  56.  
  57. struct debug_stack {
  58.     int dbLevel;
  59.     char *dbLabel;
  60.     char *dbFunc;
  61. } ;
  62. static struct debug_stack DebugStack[MAXDEBUGSTACKLEVEL];
  63. static struct debug_stack *CurDebugStack;
  64. static int DebugStackLevel;
  65. static FILE *DebugFp;
  66. static void (*DebugPrintFunc)();
  67. static void (*DebugCloseFunc)();
  68.  
  69. static void debug_def_print(fp, fmt, a1,a2,a3,a4,a5,a6,a7,a8,a9)
  70.     FILE *fp;
  71.     char *fmt;
  72. {
  73.     fprintf(fp, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
  74.     /* fflush(fp); */
  75. }
  76.  
  77. static void debug_def_close(fp)
  78.     register FILE *fp;
  79. {
  80.     fflush(fp);
  81.     fclose(fp);
  82. }
  83.  
  84. void debug_init(conf, outfile, pfunc, cfunc)
  85.     char *conf;        /* Configuration File */
  86.     char *outfile;    /* Output File */
  87.     void (*pfunc)();
  88.     void (*cfunc)();
  89. {
  90.     FILE *fp;
  91.     long clock;
  92.     DebugFp = stderr;
  93.     DebugStackLevel = 0;
  94.     if (outfile != (char *)NULL) {
  95.     if ((DebugFp = fopen(outfile, "w")) == (FILE *)NULL)
  96.         DebugFp = stderr;
  97.     }
  98.     DebugPrintFunc = (pfunc? pfunc: debug_def_print);
  99.     DebugCloseFunc = (cfunc? pfunc: debug_def_close);
  100.  
  101.     /* Read Configuration File */
  102.     if ((fp = fopen(conf, "r")) != (FILE *)NULL) {
  103.     char buf[1024];
  104.     while (fgets(buf, sizeof(buf), fp))
  105.         parse_debug_line(buf);
  106.     fclose(fp);
  107.     }
  108.     time(&clock);
  109.     DebugPrintFunc(DebugFp, "Debug Start -- %s", ctime(&clock));
  110. }
  111.  
  112. void debug_term()
  113. {
  114.     DebugCloseFunc(DebugFp);
  115. }
  116.  
  117. bool debug_setfunc(label, func)
  118.     char *label;
  119.     char *func;
  120. {
  121.     if (DebugStackLevel >= MAXDEBUGSTACKLEVEL) {
  122.     (*DebugPrintFunc)(DebugFp,
  123.               "***** Function '%s' *****\n", func);
  124.     (*DebugPrintFunc)(DebugFp, "***** STACK OVERFLOW *****\n");
  125.     return FALSE;
  126.     }
  127.     if (DebugStackLevel++ > 0)
  128.     CurDebugStack++;
  129.     else
  130.     CurDebugStack = DebugStack;
  131.     CurDebugStack->dbLabel = label;
  132.     CurDebugStack->dbFunc = func;
  133.     if ((CurDebugStack->dbLevel = debug_search_table(label, func)) > 0)
  134.     (*DebugPrintFunc)(DebugFp, "***** Function '%s' (%d) *****\n",
  135.               func, CurDebugStack->dbLevel);
  136.     return TRUE;
  137. }
  138.  
  139. void debug_endfunc(func)
  140.     char *func;
  141. {
  142.     register int depth = DebugStackLevel;
  143.     register struct debug_stack *stk = CurDebugStack;
  144.     if (depth <= 0)
  145.     return;
  146.     for ( ; depth > 0; depth--, stk--)
  147.     if (strSAME(stk->dbFunc, func)) {
  148.         CurDebugStack = stk;
  149.         DebugStackLevel = depth;
  150.         break;
  151.     }
  152.     CurDebugStack--;
  153.     DebugStackLevel--;
  154. }
  155.  
  156. #define DEBUGON(l)    (DebugStackLevel>0 && ((l)<=CurDebugStack->dbLevel))
  157.  
  158. void debug_print(level, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
  159.     int level;
  160.     char *fmt;
  161. {
  162.     if (DEBUGON(level))
  163.     (*DebugPrintFunc)(DebugFp,fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9);
  164. }
  165.  
  166. bool debug_on(level)
  167.     int level;
  168. {
  169.     return (DEBUGON(level));
  170. }
  171.  
  172. struct debug_control_table {
  173.     char *ctrlLabel;
  174.     char *ctrlFunc;
  175.     int ctrlNumber;
  176.     struct debug_control_table *ctrlNext;
  177. } ;
  178. typedef struct debug_control_table db_ctrl_tbl;
  179.  
  180. db_ctrl_tbl *DebugControlTable = (db_ctrl_tbl *)NULL;
  181.  
  182. parse_debug_line(str)
  183.     char *str;
  184. {
  185.     db_ctrl_tbl *new;
  186.     char *s;
  187.     if (!isalpha(*str))
  188.     return;
  189.     new = (db_ctrl_tbl *)memALLOC(sizeof(db_ctrl_tbl));
  190.     s = strDUP(str);
  191.     /* First Argument - Label */
  192.     new->ctrlLabel = s;
  193.     for ( ; !isspace(*s) ; s++)
  194.     if (*s == EOS) return;
  195.     *s++ = EOS;
  196.     while (isspace(*s)) s++;
  197.     /* Second Argument - Function */
  198.     new->ctrlFunc = s;
  199.     for ( ; !isspace(*s) ; s++)
  200.     if (*s == EOS) return;
  201.     *s++ = EOS;
  202.     while (isspace(*s)) s++;
  203.     /* 3rd Argument - Level */
  204.     new->ctrlNumber = atoi(s);
  205.     new->ctrlNext = DebugControlTable;
  206.     /* DebugPrintFunc(DebugFp, "DEBUG LABEL:%s, FUNC%s, LEVEL:%d\n",
  207.        new->ctrlLabel, new->ctrlFunc, new->ctrlNumber); */
  208.     DebugControlTable = new;
  209. }
  210.  
  211. int debug_search_table(label, func)
  212.     char *label;
  213.     char *func;
  214. {
  215.     db_ctrl_tbl *tb, *pending;
  216.     
  217.     for (tb = DebugControlTable, pending = (db_ctrl_tbl *)NULL;
  218.      tb != (db_ctrl_tbl *)NULL; tb = tb->ctrlNext) {
  219.     if (strSAME(tb->ctrlLabel, "*"))
  220.         continue;
  221.     if (strSAME(tb->ctrlLabel, label)) {
  222.         if (strSAME(tb->ctrlFunc, func))
  223.         return tb->ctrlNumber;
  224.         if (strSAME(tb->ctrlFunc, "*"))
  225.         pending = tb;
  226.     }
  227.     }
  228.     return(pending? pending->ctrlNumber: 0);
  229. }
  230.